﻿using Common;
using Common.Utility.AJ.Extension;
using Stylet;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AWSV2.ViewModels
{
    public class ByFieldFormulaViewModel : Screen
    {
        //加载其他窗口
        private IWindowManager windowManager;

        public string StatusBar { get; set; }
        public string By1Formula { get; set; }
        public string By2Formula { get; set; }
        public string By3Formula { get; set; }
        public string By4Formula { get; set; }
        public string By5Formula { get; set; }

        public string By6Formula { get; set; }
        public string By7Formula { get; set; }
        public string By8Formula { get; set; }
        public string By9Formula { get; set; }
        public string By10Formula { get; set; }


        public string By11Formula { get; set; }
        public string By12Formula { get; set; }
        public string By13Formula { get; set; }
        public string By14Formula { get; set; }
        public string By15Formula { get; set; }


        public string By16Formula { get; set; }
        public string By17Formula { get; set; }
        public string By18Formula { get; set; }
        public string By19Formula { get; set; }
        public string By20Formula { get; set; }

        private List<string> _numbers;
        public List<string> Numbers { get=>_numbers; set=>SetAndNotify(ref _numbers,value); }

        private AppSettingsSection config;

        public ByFieldFormulaViewModel(IWindowManager windowManager,ref AppSettingsSection cfg)
        {
            this.windowManager = windowManager;
            config = cfg;
            var numbers = new List<string>();

            for (int i = 1; i <= 20; i++)
            {
                numbers.Add(i.ToString());
                var prop = this.GetType().GetProperty($"By{i}Formula");
                prop?.SetValue(this, config.Settings[$"By{i}Formula"].TryGetString());
            }
            Numbers = numbers;

        }

        public void SaveFormula()
        {

            var array = new List<string>()
            {
                By1Formula,
                By1Formula,
                By3Formula,
                By4Formula,
                By5Formula,
                By6Formula,
                By7Formula,
                By8Formula,
                By9Formula,
                By10Formula,
                By11Formula,
                By12Formula,
                By13Formula,
                By14Formula,
                By15Formula,
                By16Formula,
                By17Formula,
                By18Formula,
                By19Formula,
                By20Formula,
            };

            for (int i = 0; i < array.Count; i++)
            {
                var index = i + 1;
                if (WordsIScn(array[i]))
                {
                    StatusBar = I18NService.Translate(LangKeys._0_包含非法字符请修改, $"{I18NService.Translate(LangKeys.备用字段)}{index}");
                    return;
                }

                config.Settings[$"By{index}Formula"].Value = array[i];
            }



            StatusBar = I18NService.Translate(LangKeys.保存成功);
        }

        private bool WordsIScn(string words)
        {
            string TmmP;

            for (int i = 0; i < words.Length; i++)
            {
                TmmP = words.Substring(i, 1);

                byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP);

                if (sarr.Length == 2)
                {
                    return true;
                }
            }
            return false;
        }

        public void ShowHelpChm()
        {
            //打开chm文档
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
            {
                FileName = Environment.CurrentDirectory + "\\无人值守汽车衡称重系统V2.2 说明书.chm",
                UseShellExecute = true,
            });
        }

        protected override void OnClose()
        {
            SaveFormula();
            base.OnClose();
        }
    }
}
